package com.huiquan.vocab.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.vocab.service.VocabElementReasonClassService;

/**
 * 原因词分类 controller
 *
 * @author leo
 * @time 2017年7月19日 下午4:31:29
 */
@Controller
@RequestMapping("/vocab")
public class VocabElementReasonClassController {

	@Autowired
	private VocabElementReasonClassService service;

	/**
	 * 进入分类列表页面
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/element_reason_class_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) {
		String tabid = req.getParameter("tabid");
		Map<String, Object> pageParam = new HashMap<>();
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		Map<String, Object> map = service.list(start, keyword, exactSearchFlag, countPerPageStr);
		ModelAndView mv = new ModelAndView("vocab/element_index", map);
		return mv;
	}

	/**
	 * 导出分类信息到excel
	 * 
	 * @param req
	 * @param resp
	 * @throws IOException
	 */
	@RequestMapping("/element_reason_class_export.do")
	public void exportToExcel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		service.exportToExcel(resp, user);
	}

	@ResponseBody
	@RequestMapping(value = "/element_reason_class_import.do")
	public ReturnData importFromExcel(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");
		return service.importFromExcel(file, user);
	}

	@ResponseBody
	@RequestMapping(value = "/element_refresh_reason_class.do")
	public ReturnData refresh(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		return service.refresh();
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/element_reason_class_sex_age_list.htm")
	public ModelAndView sexAgeList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Integer status = 0;
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("status", status.toString());
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
			String statusStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "status");
			status = Integer.parseInt(statusStr);
		}

		// 获取页面参数，如果request中有值则修改session，否则从session中拿值
		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return service.elementSexAgeList(start, keyword, status, exactSearchFlag, countPerPageStr);
	}

	@ResponseBody
	@RequestMapping(value = "/element_reason_class_set_sex.do")
	public ReturnData setSex(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String sex = req.getParameter("sex");
		return service.setSex(id, sex, user);
	}

	@ResponseBody
	@RequestMapping(value = "/element_reason_class_set_age.do")
	public ReturnData setAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String age = req.getParameter("age");
		Boolean flag = Boolean.parseBoolean(req.getParameter("flag"));
		return service.setAge(id, age, flag, user);
	}

	@ResponseBody
	@RequestMapping(value = "/element_reason_class_submit_sex_age.do")
	public ReturnData submitSexAge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String ids = req.getParameter("ids");
		return service.submitSexAge(ids, user);
	}

}
